<script setup>
import {computed, reactive, ref, toRefs} from "vue";
import {getTableRecord} from "@/api/lims/sysManager/table";
import {LimsConfig} from "@/api/lims/common/LimsModel";
import InputTable from "@/views/lims/InputTable"
import WanFengSelect from "@/components/wanfengComponents/WanFengSelect.vue";

const props = defineProps({form:Object})
const {form} = toRefs(props)
const data = reactive({
  options:{
    inspectTypeOptions:[]
  },
  rules:{
    SAMPLE_SOURCE:[{required:true, message:'样品来源必填'}],
    INSPECT_TYPE:[{required:true, message:'检验类别必填'}],
    SAMPLE_NAME:[{required:true, message:'样品名称必填'}],
    // SAMPLE_MODEL:[{required:true, message:'产品型号必填'}],
    // SAMPLE_SPECS:[{required:true, message:'产品规格必填'}],
    // SAMPLE_GEN_DATE:[{required:true, message:'生产日期必填'}],
    // SAMPLE_BATCH_NUM:[{required:true, message:'生产批号必填'}],
    // SAMPLE_PRODUCT_LEVEL:[{required:true, message:'产品等级必填'}],
    // SAMPLE_INSPECT_NUM:[{required:true, message:'检验样品数量必填'}],
  }
})
const {rules, options} = toRefs(data)
const modelTable = ref()
//规格
const queryModelSql = ref('select distinct SAMPLE_MODEL from M_CONTRACT_ANALYSIS where SAMPLE_MODEL is not null ')
const queryModelColumns = ref([{label:'规格', name:'SAMPLE_MODEL'}])
const onSelectedModel = (model) => {
  console.log('model is ::', model.SAMPLE_MODEL)
  form.SAMPLE_MODEL = model.SAMPLE_MODEL
}
const handleSelect = () => {
  let param = {sql: queryModelSql, columns: queryModelColumns.value}
  modelTable.value.show(param)
}


//型号
const specsTable = ref()
const querySpecsSql = ref('select distinct SAMPLE_SPECS from M_CONTRACT_ANALYSIS where SAMPLE_SPECS is not null')
const querySpecsColumns = ref([{label:'型号', name:'SAMPLE_SPECS'}])
const onSelectedSpecs = (specs) => {
  form.value.SAMPLE_MODEL = specs.SAMPLE_SPECS
}


const loadDictOptions = () =>{
  let config = new LimsConfig('PHRASE')
  config.addOrderBy('ORDER_NUM', 'ASC')
  config.filters.eq('PHRASE_TYPE', 'ANAL_TYPE')
  getTableRecord(config).then(response => {
    console.log(response)
    options.value.inspectTypeOptions = response.data
  })
}

const formInfo = ref()

const onSampleSourceChange = (val) =>{
  console.log('val is ::', val)
}

const sourceRules = computed(() =>{
  if (form.value.SAMPLE_SOURCE === '抽样'){
    return {
      SAMPLE_CODE_NO: [{required: true, message: '抽样编号必填'}]
    }
  }else {
    return {
      SAMPLE_CODE_NO: []
    }
  }
})

/**加载字典信息*/
loadDictOptions()

defineExpose({
  formInfo
})
</script>

<template>
  <el-form ref="formInfo" :model="form" :rules="rules" label-width="120px">
    <el-row>
      <el-col :span="12">
        <el-form-item label="样品来源" prop="SAMPLE_SOURCE">
          <el-radio-group v-model="form.SAMPLE_SOURCE" @change="onSampleSourceChange">
            <el-radio label="自主送检"></el-radio>
            <el-radio label="抽样"></el-radio>
          </el-radio-group>

        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="抽样编号" prop="SAMPLE_CODE_NO" :rules="sourceRules.SAMPLE_CODE_NO" >
          <el-input v-model="form.SAMPLE_CODE_NO" :disabled="form.SAMPLE_SOURCE !== '抽样'" placeholder="请输入 抽样编号"></el-input>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="12">
        <el-form-item label="检验类别" prop="INSPECT_TYPE">
          <el-select v-model="form.INSPECT_TYPE" filterable>
            <el-option v-for="(item,index) in options.inspectTypeOptions" :key="index" :label="item.PHRASE_TEXT" :value="item.PHRASE_ID"></el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="样品名称" prop="SAMPLE_NAME">
          <el-input v-model="form.SAMPLE_NAME"></el-input>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="12">
        <el-form-item label="产品型号" prop="SAMPLE_MODEL">
<!--          <input-table v-model="form.SAMPLE_MODEL" :sql="queryModelSql"-->
<!--                       title="选择产品型号"-->
<!--                       :columns="queryModelColumns"-->
<!--                       field="SAMPLE_MODEL"-->
<!--                       placeholder="请选择产品型号"-->
<!--          ></input-table>-->
          <wan-feng-select v-model="form.SAMPLE_MODEL" value="SAMPLE_MODEL" label="SAMPLE_MODEL"
                           table-name="M_CONTRACT_ANALYSIS"
                           where="SAMPLE_MODEL IS NOT NULL"
                           allow-create
                           placeholder="请选择产品型号"></wan-feng-select>
        </el-form-item>
        <el-form-item label="产品规格" prop="SAMPLE_SPECS">
<!--          <input-table v-model="form.SAMPLE_SPECS" :sql="querySpecsSql"-->
<!--                       title="选择产品规格"-->
<!--                       :columns="querySpecsColumns"-->
<!--                       field="SAMPLE_SPECS"-->
<!--                       placeholder="请选择产品规格"-->
<!--          ></input-table>-->
          <wan-feng-select v-model="form.SAMPLE_SPECS" value="SAMPLE_SPECS" label="SAMPLE_SPECS"
                           table-name="M_CONTRACT_ANALYSIS"
                           where="SAMPLE_SPECS IS NOT NULL"
                           placeholder="请选择产品规格"
                           allow-create
          ></wan-feng-select>
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="商标" prop="SAMPLE_BRAND">
          <el-input v-model="form.SAMPLE_BRAND"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="商标图片">
          <image-upload table-name="M_CONTRACT_ANALYSIS" :record-key="form.ORDER_ID" :is-show-tip="false" :limit="1"></image-upload>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="12">
        <el-form-item label="生产日期" prop="SAMPLE_GEN_DATE">
          <el-date-picker
              v-model="form.SAMPLE_GEN_DATE"
              type="date"
              placeholder="选择生产日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="生产批号" prop="SAMPLE_BATCH_NUM">
          <el-input v-model="form.SAMPLE_BATCH_NUM"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="产品等级" prop="SAMPLE_PRODUCT_LEVEL">
          <el-input v-model="form.SAMPLE_PRODUCT_LEVEL"></el-input>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="12">
        <el-form-item label="生产企业名称" prop="SAMPLE_PRODUCER_NAME">
          <el-input v-model="form.SAMPLE_PRODUCER_NAME"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="生产企业地址" prop="SAMPLE_PRODUCER_ADDRESS">
          <el-input v-model="form.SAMPLE_PRODUCER_ADDRESS"></el-input>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row :gutter="20">
      <el-col :span="8">
        <el-form-item label="检验样品数量" prop="SAMPLE_INSPECT_NUM">
          <el-input v-model="form.SAMPLE_INSPECT_NUM" ></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="8">
        <el-form-item label="产品库存数" prop="SAMPLE_PRODUCT_NUM">
          <el-input v-model="form.SAMPLE_PRODUCT_NUM" :disabled="form.SAMPLE_SOURCE !== '抽样'" placeholder="(仅适用于抽样)"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="8">
        <el-form-item label="抽样方式" prop="SAMPLE_METHOD">
          <el-input v-model="form.SAMPLE_METHOD" :disabled="form.SAMPLE_SOURCE !== '抽样'" placeholder="(仅适用于抽样)"></el-input>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row :gutter="20">
      <el-col :span="8">
        <el-form-item label="抽样地点" prop="SAMPLE_ADDRESS">
          <el-input v-model="form.SAMPLE_ADDRESS" :disabled="form.SAMPLE_SOURCE !== '抽样'" placeholder="(仅适用于抽样)"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="8">
        <el-form-item label="抽样人员" prop="SAMPLE_PERSON">
          <el-input v-model="form.SAMPLE_PERSON" :disabled="form.SAMPLE_SOURCE !== '抽样'" placeholder="(仅适用于抽样)"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="8">
        <el-form-item label="抽样 / 来样日期" prop="SAMPLE_DATE">
          <el-date-picker
              v-model="form.SAMPLE_DATE"
              type="date"
              placeholder="选择抽样/来样日期">
          </el-date-picker>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row :span="24">
      <el-form-item label="检验备注" prop="INSPECT_MEMO">
        <el-input type="textarea" v-model="form.INSPECT_MEMO"></el-input>
      </el-form-item>
    </el-row>
  </el-form>
</template>

<style scoped lang="scss">

</style>